package com.skyward.android.androiddemo2.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Rect;
import android.graphics.RectF;
import android.os.Handler;
import android.os.Message;
import android.support.v4.content.ContextCompat;
import android.util.AttributeSet;
import android.view.View;

import com.skyward.android.androiddemo2.R;

/**
 * Created by skyward on 2017/3/14.
 * email:
 */

public class ProgressView extends View {
    private int progress =0;
    private String aqiNum;
    private String type;
    private int numSize = 80;
    private int typeSize =40;
    private Context mContext;

    private Handler mHandler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if(msg.what == 1){
                if(progress < 100){
                    progress++;
                    invalidate();
                    mHandler.sendEmptyMessageDelayed(1,10);
                }
            }
        }
    };

    public ProgressView(Context context) {
        super(context);
        mContext = context;
    }

    public ProgressView(Context context, AttributeSet attrs) {
        super(context, attrs);
        mContext = context;
    }

    public ProgressView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mContext = context;
    }


    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        Paint paint = new Paint();
        paint.setAntiAlias(true);
        paint.setColor(ContextCompat.getColor(mContext, R.color.progress_light_color));
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeWidth(5);
        int width = getMeasuredWidth()/4;
        int height = getMeasuredHeight()/4;

        Path path = new Path();
        RectF rectF = new RectF(10,10,getMeasuredWidth()-10,getMeasuredHeight()-10);
        path.addArc(rectF,135,270);
        canvas.drawPath(path,paint);

        //绘制圆弧
        paint.setColor(ContextCompat.getColor(mContext, R.color.green_color));
        paint.setStrokeWidth(5);
        Path path2 = new Path();
        RectF rectF2 = new RectF(10,10,getMeasuredWidth()-10,getMeasuredHeight()-10);
        path2.addArc(rectF2,135,270*progress/100);
        canvas.drawPath(path2,paint);

        //绘制数字 api 或者 pm2.5的数值
        paint.setStyle(Paint.Style.FILL);
        paint.setStrokeWidth(1);
        paint.setColor(ContextCompat.getColor(mContext, R.color.green_color));
        paint.setTextSize(numSize);
        Rect rect = new Rect();
        paint.getTextBounds(aqiNum,0,aqiNum.length(),rect);
        canvas.drawText(aqiNum,getMeasuredWidth()/2 - rect.width() / 2 ,getMeasuredHeight()/2 + rect.height() / 2,paint);

        //绘制类型比如：api 或者 pm2.5
        paint.setColor(ContextCompat.getColor(mContext, R.color.progress_type_color));
        paint.setTextSize(typeSize);
        Rect rect1 = new Rect();
        paint.getTextBounds(type,0,type.length(),rect1);
        canvas.drawText(type,getMeasuredWidth()/2-rect1.width() /2,getMeasuredHeight()/2+rect1.height()+40,paint);

    }

    public void setProgress(int progress){
       this.progress = progress;
        invalidate();
//        mHandler.sendEmptyMessageDelayed(1,10);
    }

    public void setAqiNum(String aqiNum){
        this.aqiNum = aqiNum;
    }

    public void setType(String type){
        this.type = type;
    }


}
